package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/4/27.
 */

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xianzhiOA.bean.XzAttendanceStateRecord;
import com.xianzhiOA.service.XzAttendanceStateRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 考勤状态记录
 *
 * @author 王诗博
 * @create 2017-04-27 11:35
 **/
@Controller
@RequestMapping("/XzAttendanceStateRecord")
public class XzAttendanceStateRecordController {
    @Autowired
    private  XzAttendanceStateRecordService xzAttendanceStateRecordService;

    @RequestMapping("/selectAll.emp")
    public ModelAndView selectAll(String empNo,String selectMonth){
        ModelAndView mv=new ModelAndView("backEnd/attendanceShow");
        String years="";
        String months="";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");

        if(selectMonth == null){
            Date mydate=new Date();
            mv.addObject("nowData",  dateFormat.format(mydate));
            Calendar cale=Calendar.getInstance();
            years=Integer.toString(cale.get(Calendar.YEAR));
            months=Integer.toString(cale.get(Calendar.MONTH)+1);
            int monthsInt=Integer.parseInt(months);
        }else {

            mv.addObject("nowData",  selectMonth);
            String  selectMonthArry[] =selectMonth.split("-");
            years=selectMonthArry[0];
            months=selectMonthArry[1];
            int monthsInt2=Integer.parseInt(months);
            months=Integer.toString(monthsInt2);
        }
        int empNoInt=Integer.parseInt(empNo);
        XzAttendanceStateRecord xzAttendanceStateRecord=  xzAttendanceStateRecordService.selectByEmpNoAndYearAndMonth(empNoInt,years,months);
        if(xzAttendanceStateRecord != null) {
            List Arry = xzAttendanceStateRecordService.getArray(xzAttendanceStateRecord);
            mv.addObject("StateRecord", Arry);
            mv.addObject("AttendanceStateRecordId", xzAttendanceStateRecord.getAttendanceStateRecordId());
        }else {
            mv.addObject("recordIsNull","recordIsNull");
        }
        List listYearState=xzAttendanceStateRecordService.selAllByYears(years,empNoInt);
        if(listYearState.get(0).toString() == "[]"){
            mv.addObject("yearsIsNull","yearsIsNull");
        }

        List stateOneList=new ArrayList();
        List stateTwoList=new ArrayList();
        List stateThreeList=new ArrayList();
        List stateFourList=new ArrayList();
        List stateFiveList=new ArrayList();

        for(int i=0;i<listYearState.size();i++){
              if(i==0){
                  mv.addObject("stateOneList", listYearState.get(i));
              }
              if(i==1){
                  mv.addObject("stateTwoList", listYearState.get(i));
              }
              if(i==2){
                  mv.addObject("stateThreeList", listYearState.get(i));
              }
              if(i==3){
                  mv.addObject("stateFourList", listYearState.get(i));
              }
              if(i==4){
                  mv.addObject("stateFiveList", listYearState.get(i));
              }
        }

        listYearState.clear();
        mv.addObject("empNo", empNo);
        return mv;
    }

    @RequestMapping("/updateState.emp")
    public ModelAndView updateState(int stateShow,long AttendanceStateRecordId,String daysShow,String empNo,String selectMonth){
        ModelAndView mv=new ModelAndView("redirect:selectAll.emp?empNo="+empNo+"&selectMonth="+selectMonth);
        String stateShowStr= Integer.toString(stateShow);
        int i=xzAttendanceStateRecordService.updateByAttendanceStateRecordIdAndDays(AttendanceStateRecordId,daysShow,stateShowStr);
        return  mv;
    }




}
